<?php
 
$db = new Berita();
$table = "berita";
$id = htmlentities(mysql_real_escape_string($_GET['idberita']));
$berita = $db->lihat($table,$id,"");

require ('../plugins/SimpleImage.php');

if ($berita) {

	if (isset($_GET['success']) && ($_GET['success'] == "1")) {

		$o .= '<div class="alert alert-success">
				Proses tambah berita berhasil
				</div>';
				
	} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
		
		$o .= '<div class="alert alert-error">
				Proses tambah berita gagal
			   </div>';
	} elseif (isset($_GET['upload']) && ($_GET['upload'] == "0")) {
		
		$o .= '<div class="alert alert-error">
				Proses upload gambar gagal
			   </div>';
	} elseif (isset($_GET['upload']) && ($_GET['upload'] == "1")) {
		
		$o .= '<div class="alert alert-error">
				Tidak ada yang diupload
			   </div>';
	}

	$o .= '
			<form class="form-horizontal" ENCTYPE="multipart/form-data" id="registerHere" method="post" action="?page=editberita&action=edit">
			 <fieldset>
				<legend>UBAH BERITA</legend>';
	


	$js .= '<script type="text/javascript" src="../assets/js/tiny_mce/tiny_mce.js"></script>
			<script type="text/javascript">
				tinyMCE.init({
					mode : "textareas",
					theme : "simple"
				});
			</script>';

	echo $js;

	foreach ($berita as $x) {
		$o .= '
				<div class="control-group">
				  <label class="control-label" for="input01">Judul</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" id="judul" name="judul" value="'.$x['judul'].'" rel="popover" data-content="Masukkan judul berita." data-original-title="Judul Berita">
				  </div>
				</div>

				<div class="control-group">
				<label class="control-label" for="input01">Tanggal</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['tgl'].'" readonly=readonly id="tanggal" name="tanggal" rel="popover" data-content="Masukkan tanggal" data-original-title="Tanggal">
				  </div>
				</div>

				<div class="control-group">
				  <label class="control-label" for="input01">Gambar</label>
				  <div class="controls">
					<input type="file" class="input-file" id="image" name="image" rel="popover" data-content="Masukkan gambar." data-original-title="Gambar">
					<input type="hidden" value="'.$x['gambar'].'" name="gambarlama";
				  </div>
				</div>
				
				<div class="control-group">
				<label class="control-label" for="input01">Isi</label>
				  <div class="controls">
					<textarea id="elm1" name="isi" rows="15" cols="80" style="width: 80%">'.html_entity_decode($x['isi']).'</textarea>
				  </div>
				</div>
				
				<div class="control-group">
					<label class="control-label" for="input01"></label>
					  <div class="controls">
					   <button type="submit" class="btn btn-success" >Ubah Berita</button>
					  </div>
				</div>
				  </fieldset>
			</form>';
	}
} else {
	$o .= '<div class="alert alert-error">
			Data tidak ditemukan
		    </div>';
	
}

$o .= '<a href="?page=berita"><i class=" icon-arrow-left"></i> kembali</a>';

echo $o;


if ($_GET['action'] == "add") {
	
	// standarisasi ukuran foto yang di upload
	$dim  = array('W'=>300, 'H'=>250);
	$uploaddir 	= "../file/berita/$picName";
	$picName 	= basename($_FILES['image']['name']);
	$username	= $_SESSION['username'];
	
	
	if (is_uploaded_file($_FILES['image']['tmp_name'])) {
		
		if((copy($_FILES['image']['tmp_name'], $uploaddir.$picName))) {
			
			$imgType = IMAGETYPE_JPEG;
			$imgDms = getimagesize($_FILES['image']['tmp_name']);
			if ($imgDms[0] > $dim['W']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToWidth($dim['W']);
				$image->save($uploaddir.$picName, $imgType);
			} elseif ($imgDms[1] > $dim['H']) {
				$image = new SimpleImage();
				$image->load($uploaddir.$picName);
				$image->resizeToHeight($dim['H']);
				$image->save($uploaddir.$picName, $imgType);
			}
		} else {
			echo '
				<script language="javascript">
				document.location="?page=addberita&upload=0";
				</script>';
		}	
			
				$db = new Berita();
				$berita = array ('judul'=>$_POST['judul'], 
								 'tgl'=>$_POST['tanggal'], 
								 'isi'=> htmlentities(mysql_real_escape_string($_POST['isi'])),
								 'gambar'=> $picName, 
								 'nama'=> $_SESSION['username']);

				$addberita = $db->tambah("berita", $berita);

				if ($berita) {
					?>
						<script language="javascript">
						document.location="?page=addberita&success=1";
						</script>
					<?php
					
				} else {
					?>
						<script language="javascript">
						document.location="?page=addberita&success=0";
						</script>
					<?php	
				}
	} else {
		echo '
			<script language="javascript">
			document.location="?page=addberita&upload=1";
			</script>';
	}	
}